package tray;

import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.SystemTray;
import java.awt.TrayIcon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;

public class FreeMemoryTrayIcon {

    private int free;

    public void setFree(int free) {
        this.free = free;
    }

    public void run() {


        new Thread(new Runnable() {

            public void run() {
                try {
                    //execute();
                    FreeMemoryTrayIcon fmti = new FreeMemoryTrayIcon();
                    fmti.execute();
                } catch (Exception ex) {
                    Logger.getLogger(FreeMemoryTrayIcon.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }).start();

    }

    private void execute() throws Exception {

        URL imageURL = Main.class.getResource("/images/tray.png");
        TrayIcon tray = new TrayIcon(new ImageIcon(imageURL).getImage());

        MenuItem aboutItem = new MenuItem("Sobre");
        aboutItem.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                JOptionPane.showMessageDialog(null, "Ícone de bandeja powered by cobaia");
            }
        });

        MenuItem exitItem = new MenuItem("Exit");
        exitItem.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });

        PopupMenu popup = new PopupMenu();
        popup.addSeparator();
        popup.add(aboutItem);
        popup.add(exitItem);

        tray.setPopupMenu(popup);

        SystemTray.getSystemTray().add(tray);

        while (true) {
            Thread.sleep(1000);
            if (free <= 5) {
                tray.setImage(new ImageIcon(Main.class.getResource("/images/05.png")).getImage());
            } else if (free <= 10) {
                tray.setImage(new ImageIcon(Main.class.getResource("/images/10.png")).getImage());
            } else if (free <= 15) {
                tray.setImage(new ImageIcon(Main.class.getResource("/images/15.png")).getImage());
            } else if (free <= 20) {
                tray.setImage(new ImageIcon(Main.class.getResource("/images/20.png")).getImage());
            } else if (free <= 25) {
                tray.setImage(new ImageIcon(Main.class.getResource("/images/25.png")).getImage());
            } else if (free <= 30) {
                tray.setImage(new ImageIcon(Main.class.getResource("/images/30.png")).getImage());
            } else if (free <= 35) {
                tray.setImage(new ImageIcon(Main.class.getResource("/images/35.png")).getImage());
            } else if (free <= 40) {
                tray.setImage(new ImageIcon(Main.class.getResource("/images/40.png")).getImage());
            } else if (free <= 45) {
                tray.setImage(new ImageIcon(Main.class.getResource("/images/45.png")).getImage());
            } else if (free <= 50) {
                tray.setImage(new ImageIcon(Main.class.getResource("/images/50.png")).getImage());
            } else if (free <= 55) {
                tray.setImage(new ImageIcon(Main.class.getResource("/images/55.png")).getImage());
            } else if (free <= 60) {
                tray.setImage(new ImageIcon(Main.class.getResource("/images/60.png")).getImage());
            } else if (free <= 65) {
                tray.setImage(new ImageIcon(Main.class.getResource("/images/60.png")).getImage());
            } else if (free <= 70) {
                tray.setImage(new ImageIcon(Main.class.getResource("/images/70.png")).getImage());
            } else if (free <= 75) {
                tray.setImage(new ImageIcon(Main.class.getResource("/images/75.png")).getImage());
            } else if (free <= 80) {
                tray.setImage(new ImageIcon(Main.class.getResource("/images/80.png")).getImage());
            } else if (free <= 85) {
                tray.setImage(new ImageIcon(Main.class.getResource("/images/85.png")).getImage());
            } else if (free <= 90) {
                tray.setImage(new ImageIcon(Main.class.getResource("/images/90.png")).getImage());
            } else if (free <= 95) {
                tray.setImage(new ImageIcon(Main.class.getResource("/images/95.png")).getImage());
            } else {
                tray.setImage(new ImageIcon(Main.class.getResource("/images/tray.png")).getImage());
            }
        }
    }
}
